/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.allot.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.db.entity.BdTag;
import com.lightkits.common.db.entity.WmRequestLine;
import com.lightkits.common.db.entity.event.BdEvent;
import com.lightkits.framework.common.model.CheckResult;
import com.lightkits.framework.common.utility.date.DateUtil;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.domain.allot.CreateOnhAndTagService;
import com.lightkits.wms.dto.allot.model.PickHeadDto;
import com.lightkits.wms.dto.allot.model.PickLineDto;
import com.lightkits.wms.dto.stocktransfer.model.OrgSubStockViewDto;
import com.lightkits.wms.dto.stocktransfer.model.SubStockViewDTO;
import com.lightkits.wms.dto.tagservice.model.TagViewDto;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * CreateOnhAndTagServiceImpl
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/5/23 15:16
 */
@Service
public class CreateOnhAndTagServiceImpl implements CreateOnhAndTagService {

    /**
     * 创建子库存转移参数
     *
     * @param headDTO  调拨发出请求头信息
     * @param lineDTOS 调拨发出请求行信息
     * @return SubStockViewDTO
     **/
    @Override
    public CheckResult<List<SubStockViewDTO>> createSubStockParam(PickHeadDto headDTO,
                                                                  List<PickLineDto> lineDTOS) {
        CheckResult<List<SubStockViewDTO>> checkResult = new CheckResult<>();
        checkResult.setSuccessful(true);
        List<SubStockViewDTO> subStockViewDTOS = new ArrayList<>();
        for (PickLineDto line : lineDTOS) {
            SubStockViewDTO viewDTO = new SubStockViewDTO();
            viewDTO.setWorkerId(headDTO.getRequestWorkerId());
            viewDTO.setOrgId(headDTO.getOrgId());
            viewDTO.setItemId(line.getItemId());
            viewDTO.setUomCode(line.getUomCode());
            viewDTO.setLot(line.getLotNum());
            viewDTO.setOutWarehouseId(line.getFromWarehouseId());
            viewDTO.setOutWmAreaId(line.getFromAreaId());
            viewDTO.setInWarehouseId(line.getViaWarehouseId());
            viewDTO.setInWmAreaId(line.getViaAreaId());
            viewDTO.setOwnerType(line.getOwnerType());
            viewDTO.setOwnerId(line.getOwnerId());
            viewDTO.setTransferQty(line.getApplyQty());
            viewDTO.setRemark(headDTO.getRemark());
            viewDTO.setMoveTypeCode("WM_ALLOT");
            subStockViewDTOS.add(viewDTO);
        }
        checkResult.setData(subStockViewDTOS);
        return checkResult;
    }

    /**
     * 创建条码信息
     *
     * @param lines 领料单行
     * @param type  调拨类型（"SEND"或者是"RECEIVED"）
     * @param orgId 组织ID(转移的)
     * @return TagViewDto
     */
    @Override
    public CheckResult<List<TagViewDto>> createTags(List<WmRequestLine> lines,
                                                    BdEvent event,
                                                    String type,
                                                    String orgId) {
        CheckResult<List<TagViewDto>> checkResult = new CheckResult<>();
        checkResult.setSuccessful(true);
        List<TagViewDto> tagViewDTOS = new ArrayList<>();
        for (WmRequestLine dto : lines) {
            TagViewDto viewDTO = new TagViewDto();
            viewDTO.setTagCode(dto.getTagCode());
            BdTag tag = new BdTag().selectOne(new EntityWrapper().eq(BdTag.TAG_CODE, dto.getTagCode()));
            viewDTO.setTagId(tag.getId().toString());
            viewDTO.setTagType(tag.getTagType());
            viewDTO.setDocumentId(dto.getId().toString());
            viewDTO.setOwnerOrgId(orgId);
            viewDTO.setVerificationCode(tag.getVerificationCode());
            viewDTO.setDCode(tag.getDCode());
            viewDTO.setEventTime(DateUtil.toDateString(event.getCreationDate()));
            viewDTO.setEventBy(event.getWorkerId());
            viewDTO.setEventId(event.getId());
            viewDTO.setProductCode(tag.getProductCode());
            //根据调拨接收还是发出判断目标仓库
            if (type.equals("SEND")) {
                //发出是由来源仓发向同组织下的中转仓
                viewDTO.setWarehouseId(dto.getViaWarehouseId().toString());
                if (!ObjectUtil.isEmpty(dto.getViaWmAreaId())) {
                    viewDTO.setWmAreaId(dto.getViaWmAreaId().toString());
                }
            }
            if (type.equals("RECEIVED")) {
                //接收是由来源组织下的中转仓发向目标组织下的目标仓库
                viewDTO.setWarehouseId(dto.getToWarehouseId().toString());
                if (!ObjectUtil.isEmpty(dto.getToWmAreaId())) {
                    viewDTO.setWmAreaId(dto.getToWmAreaId().toString());
                }
            }
            viewDTO.setStorageUnitCode(tag.getStorageUnitCode());
            viewDTO.setTagStatus(tag.getTagStatus());
            viewDTO.setContainerOwnerType(tag.getContainerOwnerType());
            viewDTO.setContainerType(tag.getContainerType());
            viewDTO.setRemark(tag.getRemark());
            if (!ObjectUtil.isEmpty(tag.getCapacityQty())) {
                viewDTO.setCapacityQty(tag.getCapacityQty().stripTrailingZeros().toString());
            }
            if (!ObjectUtil.isEmpty(tag.getOuterTagId())) {
                viewDTO.setOuterTagId(tag.getOuterTagId().toString());
            }
            if (!ObjectUtil.isEmpty(tag.getResourceId())) {
                viewDTO.setResourceId(tag.getResourceId().toString());
            }
            if (!ObjectUtil.isEmpty(tag.getLocationId())) {
                viewDTO.setLocationId(tag.getLocationId().toString());
            }
            if (!ObjectUtil.isEmpty(tag.getContainerId())) {
                viewDTO.setContainerId(tag.getContainerId().toString());
            }
            if (!ObjectUtil.isEmpty(tag.getDocumentTypeId())) {
                viewDTO.setDocumentTypeId(tag.getDocumentTypeId().toString());
            }
            viewDTO.setItemId(dto.getItemId().toString());
            viewDTO.setTagThingType("ITEM");
            viewDTO.setUomCode(dto.getUom());
            viewDTO.setPrimaryUomCode(dto.getUom());
            if (!ObjectUtil.isEmpty(dto.getApplyQty())) {
                viewDTO.setQuantity(dto.getApplyQty().stripTrailingZeros().toString());
                viewDTO.setPrimaryQuantity(dto.getApplyQty().stripTrailingZeros().toString());
            }
            viewDTO.setLotNumber(dto.getLotNumber());
            viewDTO.setOwnerType(dto.getOwnerType());
            if (!ObjectUtil.isEmpty(dto.getOwnerId())) {
                viewDTO.setOwnerId(dto.getOwnerId().toString());
            }
            tagViewDTOS.add(viewDTO);
        }
        checkResult.setData(tagViewDTOS);


        return checkResult;
    }

    /**
     * 创建组织间的库存转移参数
     *
     * @param headDTO  调拨接收头信息
     * @param lineDTOS 调拨接收行信息
     * @return OrgSubStockViewDto
     */
    @Override
    public CheckResult<List<OrgSubStockViewDto>> createOrgSubStockParam(PickHeadDto headDTO,
                                                                        List<PickLineDto> lineDTOS) {
        CheckResult<List<OrgSubStockViewDto>> checkResult = new CheckResult<>();
        checkResult.setSuccessful(true);
        List<OrgSubStockViewDto> orgSubStockViewDtos = new ArrayList<>();

        for (PickLineDto line : lineDTOS) {
            OrgSubStockViewDto viewDTO = new OrgSubStockViewDto();
            viewDTO.setWorkerId(headDTO.getRequestWorkerId());
            viewDTO.setOutOrgId(headDTO.getOrgId());
            viewDTO.setInOrgId(headDTO.getToOrganzationId());
            viewDTO.setItemId(line.getItemId());
            viewDTO.setUomCode(line.getUomCode());
            viewDTO.setLot(line.getLotNum());
            viewDTO.setOutWarehouseId(line.getViaWarehouseId());
            viewDTO.setOutWmAreaId(line.getViaAreaId());
            viewDTO.setInWarehouseId(line.getToWarehouseId());
            viewDTO.setInWmAreaId(line.getToAreaId());
            viewDTO.setOwnerType(line.getOwnerType());
            viewDTO.setOwnerId(line.getOwnerId());
            viewDTO.setTransferQty(line.getApplyQty());
            viewDTO.setRemark(headDTO.getRemark());
            viewDTO.setMoveTypeCode("WM_ALLOT");
            orgSubStockViewDtos.add(viewDTO);
        }
        checkResult.setData(orgSubStockViewDtos);

        return checkResult;
    }
}
